﻿package beautifulWorld2D.engine {
	
	import beautifulWorld2D.engine.bo.PlanMoveObject;
	import beautifulWorld2D.engine.DecorElement;
	import beautifulWorld2D.engine.Perso;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	public class Decor{
		
		private var _decorsElements:Vector.<DecorElement>;
		private var _decorsElementsCollision:Vector.<DecorElement>;
		
		private var _Conteneur:MovieClip;
		
		
		private var _planFixe:MovieClip;
		private var _planMove:MovieClip;
		private var _planMoveVect:Vector.<PlanMoveObject>;
		private var _isCalculate:Boolean = false;
		
		private var _isZooming:Boolean = false;
		private var _isScrolling:Boolean = false;
		private var _scale:Number = 1;
		private var _sensZoom:Number = 1;
		private var _valZoom:Number = 0.02;
		private var _timeAnimScroll:Number = 1/3;
		private var _isCenterScreen:Boolean;
		private var _valScrollX:Number;
		private var _valScrollY:Number;
		private var _tabPosPerso:Array;
		private var _valRotate:Number = 2;
		private var _destRotate:Number = 0;
		private var _isRotate:Boolean = false;
		private var _sensRotate:Number = 1;
		private var _maxScaleRadius:Number;
		
		private var _widthDecor:Number;
		private var _heightDecor:Number;
		
		private var _screenRect:Sprite;
		private var _stage:Stage;
		private var _conteneurStage:DisplayObjectContainer;
		
		private var _perso:Perso;
		

		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function Decor($stage:Stage, $containerStage:DisplayObjectContainer, $containerDecor:MovieClip, planFixe:MovieClip, planMove:MovieClip){

			
			
			_conteneurStage = $containerStage;
			_stage = $stage;
			_decorsElementsCollision = new Vector.<DecorElement>();
			_decorsElements = new Vector.<DecorElement>();
			_planMoveVect = new Vector.<PlanMoveObject>();
			
			this._Conteneur	= $containerDecor;
			
			
			_widthDecor 	= _Conteneur.width > _stage.stageWidth ? _Conteneur.width : _stage.stageWidth;
			_heightDecor 	= _Conteneur.height > _stage.stageHeight ? _Conteneur.height : _stage.stageHeight;
						
			this._planFixe = planFixe;
			this._planMove = planMove;
			//this._mcProfondeur = (conteneur.parent as MovieClip).mcProfondeur;
			
			_planFixe.cacheAsBitmap = true;
			_planFixe.x = _planMove.x = _Conteneur.x = 0;
			_planFixe.y = _planMove.y = _Conteneur.y = 0;
			_conteneurStage.addChild(_planFixe);
			_conteneurStage.addChild(_Conteneur);
			
			
			addPlanMove(_planMove, 1/2);

			_screenRect = new Sprite();
			_screenRect.mouseChildren = false;
			_screenRect.mouseEnabled = false;
			_screenRect.graphics.beginFill(0xff0000, 0);
			_screenRect.graphics.drawRect(0, 0, _stage.stageWidth, _stage.stageHeight);
			_conteneurStage.addChild(_screenRect);
			
			
			if(_widthDecor > _heightDecor)
				maxScaleRadius = _widthDecor;
			else
				maxScaleRadius = _heightDecor;
			
				
				
			// recup clip 
			var vect:Vector.<DecorElement> = new Vector.<DecorElement>();
			for (var i:int = 0; i < _Conteneur.numChildren; i++)
			{
				var disp:DisplayObject 	= _Conteneur.getChildAt(i);
				
				if (disp is DecorElement)
				{
					var clip:DecorElement 	= disp as DecorElement;
					vect.push(clip);

					
				}else {
					disp.visible = false;
					//disp.parent.removeChild(disp);
				}

				
			}
			var nb:int =  vect.length;
			for (i = 0; i < nb; i++)
			{
				addDecorElement(vect[i]);
			}
			

		}
		
		public function addDecorElements($vect:Vector.<DecorElement>):void
		{
			_decorsElements = _decorsElements.concat($vect);
			checkDecorVisible();
		}
		public function addDecorElement($element:DecorElement):void
		{
			if($element.name.indexOf("collision",0) != -1)
			{
				$element.type = "collision";
				_decorsElementsCollision.push($element);
				
			}else
			{
				$element.type = "default";
				_decorsElements.push($element);
			}

			$element.xDecor = _Conteneur.x;
			$element.yDecor = _Conteneur.y;
			
			_conteneurStage.addChild($element);
			
			if ( _perso) {
				_conteneurStage.addChild(_perso.clip);
				_conteneurStage.addChild(_perso.box);
			}

			
			
			
			checkDecorVisible();
		}
		public function removeDecorElement($element:DecorElement):void
		{
			var nb:int = _decorsElements.length;
			for (var i:int = 0; i < nb; i++) 
			{

				if ( _decorsElements[i] == $element) {
					
					_decorsElements.splice(i, 1);
					$element.disable();
					$element.dispose();
					_conteneurStage.removeChild($element);
					break;
				}
			}
			
			
		}
		public function addPlanMove($element:MovieClip, $ratioSpeed:Number):void
		{
			var vect:Vector.<DecorElement> = new Vector.<DecorElement>();
			for (var i:int = 0; i < $element.numChildren; i++)
			{
				var disp:DisplayObject 	= $element.getChildAt(i);
				
				if (disp is DecorElement)
				{
					var clip:DecorElement 	= disp as DecorElement;
					vect.push(clip);

					
				}else {
					disp.visible = false;
					//disp.parent.removeChild(disp);
				}
			}
			var pl:PlanMoveObject = new PlanMoveObject();
			pl.rationSpeed = $ratioSpeed;
			pl.vect = vect;
			_planMoveVect.unshift(pl);
			
			_conteneurStage.addChild(_planFixe);
			
			var nb:int =  _planMoveVect.length;
			for (var i:int = 0; i < nb; i++)
			{
				for (var j:int = 0; j < _planMoveVect[i].vect.length; j++)
				{
					_conteneurStage.addChild(_planMoveVect[i].vect[j]);
				}
			}
			
			nb =  _decorsElements.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElements[i]);
			}
			nb =  _decorsElementsCollision.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElementsCollision[i]);
			}
			
			if ( _perso) {
				_conteneurStage.addChild(_perso.clip);
				_conteneurStage.addChild(_perso.box);
			}
		}
		
		/*public function addPlanMove1($element:MovieClip):void
		{
			
			var vect:Vector.<DecorElement> = new Vector.<DecorElement>();
			for (var i:int = 0; i < $element.numChildren; i++)
			{
				var disp:DisplayObject 	= $element.getChildAt(i);
				
				if (disp is DecorElement)
				{
					var clip:DecorElement 	= disp as DecorElement;
					vect.push(clip);

					
				}else {
					disp.visible = false;
					//disp.parent.removeChild(disp);
				}

				
			}
			_conteneurStage.addChild(_planFixe);
			var nb:int =  vect.length;
			for (var i:int = 0; i < nb; i++)
			{
				_planMoveVect.push(vect[i]);
				_conteneurStage.addChild(vect[i]);
			}

			//_conteneurStage.addChild(_Conteneur);
			nb =  _decorsElements.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElements[i]);
			}
			nb =  _decorsElementsCollision.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElementsCollision[i]);
			}
			
			if ( _perso) {
				_conteneurStage.addChild(_perso.clip);
				_conteneurStage.addChild(_perso.box);
			}

		}
		public function addPlanMove2($element:MovieClip):void
		{
			
			_planMove2 = $element;
			
			var vect:Vector.<DecorElement> = new Vector.<DecorElement>();
			for (var i:int = 0; i < $element.numChildren; i++)
			{
				var disp:DisplayObject 	= $element.getChildAt(i);
				
				if (disp is DecorElement)
				{
					var clip:DecorElement 	= disp as DecorElement;
					vect.push(clip);

					
				}else {
					disp.visible = false;
					//disp.parent.removeChild(disp);
				}

				
			}
			_conteneurStage.addChild(_planFixe);
			var nb:int =  vect.length;
			for (var i:int = 0; i < nb; i++)
			{
				_planMoveVect2.push(vect[i]);
				_conteneurStage.addChild(vect[i]);
			}
			
			nb =  _planMoveVect.length;
			for (i = 0; i < nb; i++) {
				_conteneurStage.addChild(_planMoveVect[i]);
			}
			
			//_conteneurStage.addChild(_Conteneur);
			nb =  _decorsElements.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElements[i]);
			}
			nb =  _decorsElementsCollision.length;
			for (i = 0; i < nb; i++) 
			{
				_conteneurStage.addChild(_decorsElementsCollision[i]);
			}
			
			if ( _perso) {
				_conteneurStage.addChild(_perso.clip);
				_conteneurStage.addChild(_perso.box);
			}

		}*/
		public function checkDecorVisible():void 
		{
			
			for each(var p:DecorElement in _decorsElementsCollision)
			{
				if ( p.hitTestObject(_screenRect)) {
					p.inScreen = true;
				}else {
					p.inScreen = false;
				}
			}
			for each(p in _decorsElements)
			{
				if ( p.hitTestObject(_screenRect)) {
					p.inScreen = true;
				}else {
					p.inScreen = false;
				}
			}
			for each(var pl:PlanMoveObject in _planMoveVect)
			{
				for each(p in pl.vect)
				{
					if ( p.hitTestObject(_screenRect)) {
						p.inScreen = true;
					}else {
						p.inScreen = false;
					}
				}
			}

		}
		
		private function onMouseDownClipDrag(evt:MouseEvent){
			evt.currentTarget.startDrag();
		}
		private function onMouseUpClipDrag(evt:MouseEvent){
			evt.currentTarget.stopDrag();
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function get x():Number{
			return this._Conteneur.x ;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function get y():Number{
			return this._Conteneur.y ;
		}
		
		public function get widthDecor():Number { return _widthDecor; }
		
		public function set widthDecor(value:Number):void 
		{
			_widthDecor = value;
		}
		
		public function get heightDecor():Number { return _heightDecor; }
		
		public function set heightDecor(value:Number):void 
		{
			_heightDecor = value;
		}
		
		public function get planFixe():MovieClip { return _planFixe; }
		
		public function get isZooming():Boolean { return _isZooming; }
		
		public function set isZooming(value:Boolean):void 
		{
			_isZooming = value;
		}
		
		public function get isScrolling():Boolean { return _isScrolling; }
		
		public function set isScrolling(value:Boolean):void 
		{
			_isScrolling = value;
		}
		
		public function get planMove():MovieClip { return _planMove; }
		
		public function set planMove(value:MovieClip):void 
		{
			_planMove = value;
		}
		public function set planMoveVisible(value:Boolean):void 
		{
			for each(var pl:PlanMoveObject in _planMoveVect)
			{
				for each(var p:DecorElement in pl.vect)
				{
					p.visible = value;
				}
				
				
			}
		}

		public function get scale():Number { return _scale; }
		
		public function set scale(value:Number):void 
		{
			_scale = value;
		}
		
		public function get maxScaleRadius():Number { return _maxScaleRadius; }
		
		public function set maxScaleRadius(value:Number):void 
		{
			_maxScaleRadius = value;
		}
		
		public function get sensRotate():Number { return _sensRotate; }
		
		public function set sensRotate(value:Number):void 
		{
			_sensRotate = value;
		}
		
		public function get isRotate():Boolean { return _isRotate; }
		
		public function set isRotate(value:Boolean):void 
		{
			_isRotate = value;
		}
		
		public function get destRotate():Number { return _destRotate; }
		
		public function set destRotate(value:Number):void 
		{
			_destRotate = value;
		}
		
		public function get tabPosPerso():Array { return _tabPosPerso; }
		
		public function set tabPosPerso(value:Array):void 
		{
			_tabPosPerso = value;
		}
		
		public function get isCenterScreen():Boolean { return _isCenterScreen; }
		
		public function set isCenterScreen(value:Boolean):void 
		{
			_isCenterScreen = value;
		}
		
		
		
		


		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function getPointCollision_X(posBox_x:Number,posBox_y:Number,sens:Number, clip:MovieClip = null):Number{
			var flag = false;
			var point:Number = -1;
			var v:Number = -1;
			
			var myPoint:Point = new Point(posBox_x, posBox_y);
			myPoint = _Conteneur.localToGlobal(myPoint);
			
			
			// calcul point collision horizontal
			/*while (_Decor.hitTestPoint(myPoint.x, myPoint.y, true)) {
				posBox_x -= 1*sens;
				v = posBox_x;
				//trace("calculate collision x : " + v);
				myPoint = new Point(posBox_x, posBox_y);
				myPoint = _Conteneur.localToGlobal(myPoint);
				_isCalculate = true;
				flag = true;
			}

			if (!_Decor.hitTestPoint(myPoint.x+(1*sens), myPoint.y, true)) {
				point = v;
			} else {
				point = posBox_x;
				_isCalculate = false;
				flag = true;
			}*/
			
			
			// test collision spéciales
			var nb:int = _decorsElementsCollision.length;
			for(var i = 0;i < nb && !flag ; i++){
				if (_decorsElementsCollision[i].type == "collision" && (clip == null || (clip != null && this._decorsElementsCollision[i] != clip)))
				{
					var flag5:Boolean = false;
					if(_decorsElementsCollision[i].type == "move" && (clip == null || (clip != null && clip.type != "move" && _decorsElementsCollision[i].inScreen))) flag5 = true;

					while (this._decorsElementsCollision[i].zone.hitTestPoint(myPoint.x, myPoint.y, true)) {
						posBox_x -= 1*sens;
						v = posBox_x;
						//trace("calculate collision x : " + v);
						myPoint = new Point(posBox_x, posBox_y);
						myPoint = _Conteneur.localToGlobal(myPoint);
						_isCalculate = true;
						flag = true;
					}

					if (!this._decorsElementsCollision[i].zone.hitTestPoint(myPoint.x+(1*sens), myPoint.y, true)) {
						point = v;
					} else {
						point = posBox_x;
						_isCalculate = false;
						flag = true;
					}
					

				}
			}
			
			
			return point;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function getPointCollision_Sol(posBox_x:Number,posBox_y:Number,posBoxy_Start:Number, isRun:Boolean, clip:MovieClip = null):Array{
			
			var flag = false;
			var pointTab:Array = new Array();
			pointTab[0] = -1;
			pointTab[1] = -1;
			
			var x:Number = -1;
			var y:Number = -1;
			
			var myPoint:Point;
			var intervalTestY:Number;
			
			for(var posy= posBox_y; posy <=  posBox_y && !flag;posy+=6){
				myPoint = new Point(int(posBox_x), int(posy));
				myPoint = _Conteneur.localToGlobal(myPoint);
				myPoint.x = int(myPoint.x);
				myPoint.y = int(myPoint.y);
				// calcul point collision vertical
				/*while (_Decor.hitTestPoint(myPoint.x, myPoint.y, true)) {
					posBox_y -= 1;
					y = posBox_y;
					//trace("calculate collision y : " + y);
					myPoint = new Point(posBox_x, posBox_y);
					myPoint = _Conteneur.localToGlobal(myPoint);
					_isCalculate = true;
					flag = true;
				}
				if (!_Decor.hitTestPoint(myPoint.x, myPoint.y+1, true)) {
					pointTab[1] = y;
				} else {
					pointTab[1] = posBox_y;
					flag = true;
					_isCalculate = false;
				}*/
				
				
				
				// test collision 
				var nb:int = _decorsElementsCollision.length;
				for(var i = 0;i <nb && !flag; i++){
					if(_decorsElementsCollision[i].type == "collision" && clip != this._decorsElementsCollision[i] && _decorsElementsCollision[i].inScreen){
						// test inclinaison sol
						/*if(!isRun && this._decorsElements[i].hitTestPoint(myPoint.x, myPoint.y, true)){
							if(!this._decorsElements[i].hitTestPoint(myPoint.x + 5, myPoint.y, true)){
								posBox_x += 5;
								x = posBox_x;
								pointTab[0] = x;
							}else{
								if(!this._decorsElements[i].hitTestPoint(myPoint.x - 5, myPoint.y, true)){
									posBox_x -= 5;
									x = posBox_x;
									pointTab[0] = x;
								}
							}
						}*/
						
						
						while (this._decorsElementsCollision[i].zone.hitTestPoint(myPoint.x, myPoint.y, true)) {
							posBox_y -= 1;
							y = posBox_y;
							myPoint = new Point(int(posBox_x), int(posBox_y));
							myPoint = _Conteneur.localToGlobal(myPoint);
							myPoint.x = int(myPoint.x);
							myPoint.y = int(myPoint.y);
							_isCalculate = true;
							flag = true;
						}
						if (!this._decorsElementsCollision[i].zone.hitTestPoint(myPoint.x, myPoint.y+1, true)) {
							pointTab[1] = y;
						} else {
							pointTab[1] = posBox_y;
							flag = true;
							_isCalculate = false;
						}
					}
				}
			}
		
			
			
			return pointTab;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function gravityDecor(evt:Event = null) {
			/*var nb:int = _decorsElementsCollision.length;
			for(var i = 0;i < nb; i++){
				if(_decorsElementsCollision[i].type == "move" && _decorsElementsCollision[i].inScreen){
					var flag:Boolean = false;
					var pointCollisionTab:Array;
					for(var posx = _decorsElements[i].x; posx <= _decorsElements[i].x && !flag; posx++){
						pointCollisionTab = getPointCollision_Sol(posx, _decorsElements[i].y+10, _decorsElements[i].y, false, _decorsElements[i]);
						if ( pointCollisionTab[1] == -1) {
							flag = false;
						}else{
							//trace("gravityDecor touche sol");
							flag = true;
							if(pointCollisionTab[1] != -1) _decorsElements[i].y = pointCollisionTab[1];
						}
					}
					if(!flag){
						_decorsElements[i].y += 10;
					}
				}
			}*/
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function testDecor($box:DisplayObject):Vector.<DecorElement>
		{
			
			var vectDecor:Vector.<DecorElement> = _decorsElements.concat(_decorsElementsCollision);
			var nb:int = vectDecor.length;
			var vect:Vector.<DecorElement> = new Vector.<DecorElement>();
			for (var i = 0; i < nb; i++) {
				
				
					if ( vectDecor[i].inScreen) {
						
						if (vectDecor[i].zone.hitTestObject($box))
						{
							vect.push(vectDecor[i]);
						}
					}
			}
			
			return vect;
		}
		


		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		private function popTab(tab:Array, idToPop:Number):Array{

			for(var i=idToPop;i<tab.length-1;i++){
				//trace("popTab");
				tab[i] = tab[i+1];
			}
			tab.pop();
			
			return tab;
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function getPosCenterScreen(posPersoX:Number, posPersoY:Number, mcToCenter:MovieClip = null):Array{
			var clipToCenter:MovieClip = _Conteneur;
			if(mcToCenter != null) clipToCenter = mcToCenter;
			
			var tab:Array = new Array();
			
			var minWidth  = - _widthDecor*2 + _stage.stageWidth;
			var minHeight = - _heightDecor*2 +  _stage.stageHeight;
			var maxWidth = _widthDecor ;
			var maxHeight = _heightDecor;
				
			var myPoint:Point = new Point(posPersoX, posPersoY);
			myPoint = clipToCenter.localToGlobal(myPoint);
			
			

			
			
			var tranlateX:Number = _stage.stageWidth*3/8 - myPoint.x;
			var tranlateY:Number = _stage.stageHeight*5/8 - myPoint.y;
			
			
			
			// translate x
			if(clipToCenter.x + tranlateX < minWidth){
				tab[0] = minWidth;
			}else{
				if(clipToCenter.x + tranlateX > maxWidth)
					tab[0] = maxWidth;
				else
					tab[0] = clipToCenter.x + tranlateX;
			}
			
			// translate y
			if(clipToCenter.y + tranlateY < minHeight){
				tab[1] = minHeight;
			}else{
				if(clipToCenter.y + tranlateY > maxHeight)
					tab[1] = maxHeight;
				else
					tab[1] = clipToCenter.y + tranlateY;
			}
			
			
			
			
			return tab;

		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setZoom(val:Number){
			

				isZooming = true;
				_valZoom = 0.02;

				if(val < 0) _sensZoom = -1;
				else _sensZoom = 1;
			
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function setRotate(val:Number){
				var cliToRotate:MovieClip = _Conteneur.decor;
				

				isRotate = true;
				_valRotate = 2;
				destRotate = cliToRotate.rotation + val;
				
				if(val < 0) _sensRotate = -1;
				else _sensRotate = 1;
			
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function endZooming(){

			isZooming = false;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function endRotate(){

			isRotate = false;
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function animZoom(x_toZoom:Number, y_toZoom:Number){
			
			//if(!_isCalculate){


				if(_Conteneur.scaleX + _sensZoom*_valZoom > 3){
					_Conteneur.scaleY = _Conteneur.scaleX = 3;
					endZooming();
				}else{
					if(_Conteneur.scaleX + _sensZoom*_valZoom < 0.2){
						_Conteneur.scaleX = _Conteneur.scaleY = 0.2;
						endZooming();
					}else{
						if(_valZoom > 0){
							_Conteneur.scaleX +=  _sensZoom*_valZoom;
							_Conteneur.scaleY +=  _sensZoom * _valZoom;
							//_planMove.scaleX =  _Conteneur.scaleX;
							//_planMove.scaleY =  _Conteneur.scaleY;
							_valZoom -= 0.001;
						}else{
							endZooming();
						}
					}

				}
				_scale = _Conteneur.scaleX;
				//if(_scale < 60) _mcProfondeur.alpha = ((100-_scale)*25)/100;
				//if(_scale >= 60 && _mcProfondeur.alpha != 0) _mcProfondeur.alpha = 0;
				
				// position
				tabPosPerso = getPosCenterScreen(x_toZoom, y_toZoom);
				_Conteneur.x = tabPosPerso[0];
				_Conteneur.y = tabPosPerso[1];
				
				
				refreshPositionDecorElements();
			//}
			
			
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function animRotation(x_toRotate:Number, y_toRotate:Number){
			
			var clipToRotate:MovieClip = _Conteneur.decor;
			
			//if(!_isCalculate){
				//trace("animRotation " + destRotate + " " + clipToRotate.rotation + " " + _sensRotate );	
					
				if(_sensRotate > 0){
					if(clipToRotate.rotation + _valRotate < destRotate){
						clipToRotate.rotation += _valRotate;
						//_valRotate -= _sensRotate*0.1;
					}else{
						clipToRotate.rotation =  destRotate;
						endRotate();
					}	
				}else{
					if(clipToRotate.rotation - _valRotate > destRotate){
						clipToRotate.rotation -= _valRotate;
						//_valRotate -= _sensRotate*0.1;
					}else{
						clipToRotate.rotation =  destRotate;
						endRotate();
					}
				}
				// position cliToRotate
				var myPoint:Point = new Point(x_toRotate, y_toRotate);
				myPoint = _Conteneur.localToGlobal(myPoint);
				
				var myPoint2:Point = new Point(myPoint.x, myPoint.y);
				myPoint2 = clipToRotate.globalToLocal(myPoint2);
				
				var tabPos = getPosCenterScreen(x_toRotate - clipToRotate.x, y_toRotate - clipToRotate.y, clipToRotate);
				clipToRotate.x = tabPos[0];
				clipToRotate.y = tabPos[1];

			//}
			
			
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		public function animCenterScreen(posPersoX:Number, posPersoY:Number, $tween:Boolean = true){

			
				
			if(!isZooming && !isRotate){
				
				tabPosPerso = getPosCenterScreen(posPersoX, posPersoY);

				if ( $tween) {
					var diff_x:Number = tabPosPerso[0] - _Conteneur.x;
					var diff_y:Number = tabPosPerso[1] - _Conteneur.y;

					if(Math.abs(diff_y) >= 10){
						isCenterScreen = true;
						_valScrollY = Math.round(diff_y/(_timeAnimScroll/(Engine.timerRepeat/1000)));
					}

					if(Math.abs(diff_x) >= 10){
						isCenterScreen = true;
						_valScrollX = Math.round(diff_x/(_timeAnimScroll/(Engine.timerRepeat/1000)));
					}
					
					
					if(isCenterScreen){
						f_animCenterScreen(_Conteneur, "x", tabPosPerso[0], _valScrollX);
						f_animCenterScreen(_Conteneur, "y", tabPosPerso[1], _valScrollY);
						

					}
				}else {
					_Conteneur.x = tabPosPerso[0];
					_Conteneur.y = tabPosPerso[1];
				}

			
			}
			
			// decor fixe
			this._planFixe.y = int(_stage.stageHeight*5/8);
			this._planFixe.x = 0;
			
			
			//decor move axe X et Y
			var minWidth:Number  		= - _widthDecor*2 + _stage.stageWidth;
			var maxWidth:Number	     	= _widthDecor;
			var minHeight:Number  		= - _heightDecor*2 + _stage.stageHeight;
			var maxHeight:Number	    = _heightDecor;
			

			
			//_planMove.x = int(0.3 * _Conteneur.x); // (_Conteneur.x /  ( - minWidth + maxWidth))  * (_planMove.width - _stage.stageWidth);
			//_planMove.y =  int(_stage.stageHeight * 4 / 8); // (_Conteneur.y /  ( - minHeight + maxHeight)) * 0.3  * (_planMove.height - _stage.stageHeight);
					
			refreshPositionDecorElements();
		}
		
		public function onResize():void 
		{
			_screenRect.width = _stage.stageWidth;
			_screenRect.height = _stage.stageHeight;
			
			
			
			checkDecorVisible();
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		/////
		////////////////////////////////////////////////////////////////////////////////////////////
		private function f_animCenterScreen(target:MovieClip, prop:String, destvalue:Number,_valCenterScreen: Number){
			
			//trace("f_animCenterScreen " + _valCenterScreen);
			
			if(_valCenterScreen >= 0){
				if(target[prop] + _valCenterScreen > destvalue){
					target[prop]  = destvalue;
					isCenterScreen = false;
				}else{
					target[prop]  += _valCenterScreen;
				}
			}else{
				if (target[prop]  + _valCenterScreen < destvalue)
				{
					target[prop]  = destvalue;
					isCenterScreen = false;
				}else{
					target[prop]  += _valCenterScreen;
				}
			}

		}
        public function scaleAroundPoint($objToScale:DecorElement, $regX:int, $regY:int, $scaleX:Number, $scaleY:Number):void{
            if (!$objToScale){
                return;
            }
           
            var scaleXCorrec:Number = $scaleX* (1 / $objToScale.scaleX);
            var scaleYCorrec:Number = $scaleY * (1 / $objToScale.scaleY);
           
            var transformedVector:Point = new Point( ($regX-$objToScale.xStage)*scaleXCorrec, ($regY-$objToScale.yStage)*scaleYCorrec );         
            $objToScale.xStage = $regX-( transformedVector.x);
            $objToScale.yStage = $regY-( transformedVector.y);
            $objToScale.scaleDecor =  $scaleX;
        }
		private function refreshPositionDecorElements( ):void
		{
			var nb:int =  _decorsElements.length;
			for (var i:int = 0; i < nb; i++) 
			{
				var item:DecorElement = _decorsElements[i];
				item.scaleDecor = _scale;
				item.xDecor = _Conteneur.x;
				item.yDecor = _Conteneur.y;
				
				if ( item.hitTestObject(_screenRect)) {
					item.inScreen = true;
				}else {
					item.inScreen = false;
				}
			}
			nb =  _decorsElementsCollision.length;
			for (i = 0; i < nb; i++) 
			{
				item = _decorsElementsCollision[i];
				item.scaleDecor = _scale;
				item.xDecor = _Conteneur.x;
				item.yDecor = _Conteneur.y;

				
				if ( item.hitTestObject(_screenRect)) {
					item.inScreen = true;
				}else {
					item.inScreen = false;
				}
			}
			
			nb =  _planMoveVect.length;
			var pl:PlanMoveObject;
			var nb2:int;
			for (i = 0; i < nb; i++) 
			{
				pl = _planMoveVect[i];
				nb2 = pl.vect.length;
				for (var j:int = 0; j < nb2; j++) 
				{
					item = pl.vect[j];
					item.scaleDecor = _scale;
					item.xDecor = int(pl.rationSpeed * _Conteneur.x);
					//item.yDecor = int(_stage.stageHeight * (4 / 8) + pl.rationSpeed*_Conteneur.y);
					item.yDecor = int(pl.rationSpeed*_Conteneur.y);
				}
				if ( item.hitTestObject(_screenRect)) {
					item.inScreen = true;
				}else {
					item.inScreen = false;
				}
			}
			
			
			
			if ( _perso) {
				_perso.scaleDecor = _scale;
				_perso.xDecor = _Conteneur.x;
				_perso.yDecor = _Conteneur.y;
			}

			
		}
		
		public function destroy():void
		{
			var nb:int =  _decorsElements.length;
			var item:DecorElement;
			for (var i:int = 0; i < nb; i++) 
			{
				item = _decorsElements[i];
				item.disable();
				item.dispose();
				if( item.parent) item.parent.removeChild(item);
				
			}
			nb =  _decorsElementsCollision.length;
			for (i = 0; i < nb; i++) 
			{
				item = _decorsElementsCollision[i];
				item.disable();
				item.dispose();
				if( item.parent) item.parent.removeChild(item);
			}
			nb =  _planMoveVect.length;
			for (i = 0; i < nb; i++) 
			{
				for (var j:int = 0; j < _planMoveVect[i].vect.length; j++) 
				{
				}
				if( _planMoveVect[i].vect[j].parent) _planMoveVect[i].vect[j].parent.removeChild(_planMoveVect[i].vect[j]);
			}

			
			if( _Conteneur.parent) _Conteneur.parent.removeChild(_Conteneur);
			if( _planFixe.parent) _planFixe.parent.removeChild(_planFixe);
			if ( _planMove.parent) _planMove.parent.removeChild(_planMove);
			
			if ( _perso ) {
				_conteneurStage.removeChild(_perso.clip);
				_conteneurStage.removeChild(_perso.box);
			}
			
			_Conteneur = null;
			_planFixe = null;
			_planMove = null;
			_decorsElements = null;
			_stage = null;
		}
		
		public function addPerso($perso:Perso):void
		{
			_perso = $perso;
			
			_perso.xDecor = _Conteneur.x;
			_perso.yDecor = _Conteneur.y;
			
			_conteneurStage.addChild(_perso.clip);
			_conteneurStage.addChild(_perso.box);
		}
			

	}
}